<?php
/**
 * Eka Framework 0.3.0
 *
 * @copyright Copyright (c) 2008 Alexey Yu. Chuprina <656b61@gmail.com> (http://php-eka.googlecode.com)
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
 *
 * @version $Id$
 */

require_once 'Temply.php';
require_once 'Eka/Application/View/Interface.php';

/**
 * Eka_Application_View_Temply
 *
 * @package Application
 * @subpackage View
 */
class Eka_Application_View_Temply extends Temply implements Eka_Application_View_Interface
{
	/**
	 * @var Eka_Application_Controller
	 */
	protected $controller;
	
	/**
	 * Layout file name
	 *
	 * @var string
	 */
	protected $layout;
	
	/**
	 * Layout class name
	 *
	 * @var string
	 */
	protected $layoutClass = 'Eka_Application_View_Temply';
	
	/**
	 * @var bool
	 */
	protected $useLayout = true;
	
	/**
	 * Only layout variables
	 *
	 * @var array
	 */
	protected $layoutVars = array();
	
	public function __construct(Eka_Application_Controller $controller, $file, $layout = null)
	{
		parent::__construct($file, true);
		
		$this->controller = $controller;
		$this->layout = $layout;
		
		$this->init();
	}
	
	protected function init()
	{}
	
	/**
	 * @return Eka_Application_Controller
	 */
	public function getController()
	{
		return $this->controller;
	}
	
	/**
	 * @return Eka_Application
	 */
	public function getApplication()
	{
		return $this->controller->getApplication();
	}

	/**
	 * @return Eka_Application_Request
	 */
	public function getRequest()
	{
		return $this->controller->getRequest();
	}

	/**
	 * @return Eka_Application_Response
	 */
	public function getResponse()
	{
		return $this->controller->getResponse();
	}

	/**
	 * Use layout or not
	 *
	 * @param bool $is
	 * @return bool
	 */
	public function useLayout($is = null)
	{
		if (null !== $is) {
			$old = $this->useLayout;
			$this->useLayout = (bool)$old;
			return $old;
		} else {
			return $this->useLayout;
		}
	}
	
	/**
	 * Assign layout variables
	 *
	 * @param string|array $key Variable name or array of key => value pairs
	 * @param mixed $value (Optional) If assigning a named variable, use this as the value
	 * @return Eka_Application_View_Temply
	 */
	public function setLayoutVar($key, $value = null)
	{
		if (is_array($key)) {
			foreach ($key as $k => $v) {
				$this->layoutVars[$k] = $v;
			}
		} else {
			$this->layoutVars[$key] = $value;
		}
		return $this;
	}
	
	/**
	 * Get assigned layout variable value
	 *
	 * @param string $key Variable name
	 * @return mixed
	 */
	public function getLayoutVar($key)
	{
		return isset($this->layoutVars[$key]) ? $this->layoutVars[$key] : null;
	}
	
	public function execute()
	{
		#TODO может лучьше переопределить эти значение через __get() и использовать для лейаута этот класс а не Temply?
		$this->set('_controller', $this->getController());
		$this->set('_application', $this->getRequest());
		$this->set('_request', $this->getRequest());
		$this->set('_response', $this->getRequest());
		
		if (null !== $this->layout && $this->useLayout) {
			$content = parent::execute();
			$layout = new $this->layoutClass($this->controller, $this->layout);
			if (count($this->layoutVars) !== 0) {
				$layout->set($this->layoutVars);
			}
			$layout->set('_CONTENT_', $content);
			return $layout->execute();
		} else {
			return parent::execute();
		}
	}
}
